﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Web.UI.HtmlControls;
using System.Text;

namespace CoolERP_Web.User
{
    public partial class YSKZD_Query : BasePage
    {
        private int iStatus
        {
            get { return this.ViewState["iStatus"] == null ? 1 : Convert.ToInt32(this.ViewState["iStatus"]); }
            set { this.ViewState["iStatus"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AspNetPager1.PageSize = this.GridView1.PageSize;
                BindData();
            }
        }


        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        protected void LinkButton_Command(object sender, CommandEventArgs e)
        {
            iStatus = Convert.ToInt32(e.CommandArgument);
            this.AspNetPager1.CurrentPageIndex = 1;
            //GridView1.Columns[0].Visible = GridView1.Columns[11].Visible = iStatus == 0;
            BindData();
        }

        private void BindData()
        {
            if (iStatus == 0)
            {
                GridView1.Visible = true;
                GridView2.Visible = false;
                GridView3.Visible = false;
                int HTZT = 1;   //此时的销售合同应为已确认的销售合同
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("HTZT", HTZT.ToString());
                using (CW_YSKZDBLL xBLL = new CW_YSKZDBLL())
                {
                    int RecordCount = 0;
                    List<vXSHT> itemLists = xBLL.GetXSHT("BH", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                    this.AspNetPager1.RecordCount = RecordCount;
                    GridView1.DataSource = itemLists;
                    GridView1.DataBind();
                    this.AspNetPager1.Visible = RecordCount > 0;
                    panMsg.Visible = !this.AspNetPager1.Visible;
                }
            }
            else if (iStatus == 1)
            {
                GridView2.Visible = true;
                GridView1.Visible = false;
                GridView3.Visible = false;
                int i = 0;
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("ZDZT", i.ToString());
                if (!string.IsNullOrEmpty(tbBH.Text.Trim()))
                dc.Add("BH",tbBH.Text.Trim());
                if(!string.IsNullOrEmpty(tbKHJC.Text.Trim()))
                dc.Add("KHJC",tbKHJC.Text.Trim());
                if(!string.IsNullOrEmpty(tbCZRYName.Text.Trim()))
                dc.Add("CZRYName",tbCZRYName.Text.Trim());
                if (!string.IsNullOrEmpty(tbCJSJ.Text.Trim()))
                dc.Add("CJSJ", tbCJSJ.Text.Trim());
                if (!string.IsNullOrEmpty(tbCKSJ.Text.Trim()))
                dc.Add("CKSJ", tbCKSJ.Text.Trim());
                if(!string.IsNullOrEmpty(tbYSZEMin.Text.Trim()))
                dc.Add("YSZEMin",tbYSZEMin.Text.Trim());
                if(!string.IsNullOrEmpty(tbYSZEMax.Text.Trim()))
                dc.Add("YSZEMax",tbYSZEMax.Text.Trim());
                using (CW_YSKZDBLL yskzdB = new CW_YSKZDBLL())
                {
                    int RecordCount = 0;
                    List<YSKZDModel> itemLists = yskzdB.GetYSKZD(dc, "BH", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                    this.AspNetPager1.RecordCount = RecordCount;
                    GridView2.DataSource = itemLists;
                    GridView2.DataBind();
                    this.AspNetPager1.Visible = RecordCount > 0;
                    panMsg.Visible = !this.AspNetPager1.Visible;
                }
            }
            else if (iStatus == 2)
            {
                GridView3.Visible = true;
                GridView1.Visible = false;
                GridView2.Visible = false;
                int i=1;
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("ZDZT", i.ToString());
                if (!string.IsNullOrEmpty(tbBH.Text.Trim()))
                    dc.Add("BH", tbBH.Text.Trim());
                if (!string.IsNullOrEmpty(tbKHJC.Text.Trim()))
                    dc.Add("KHJC", tbKHJC.Text.Trim());
                if (!string.IsNullOrEmpty(tbCZRYName.Text.Trim()))
                    dc.Add("CZRYName", tbCZRYName.Text.Trim());
                if (!string.IsNullOrEmpty(tbCJSJ.Text.Trim()))
                    dc.Add("CJSJ", tbCJSJ.Text.Trim());
                if (!string.IsNullOrEmpty(tbCKSJ.Text.Trim()))
                    dc.Add("CKSJ", tbCKSJ.Text.Trim());
                if (!string.IsNullOrEmpty(tbYSZEMin.Text.Trim()))
                    dc.Add("YSZEMin", tbYSZEMin.Text.Trim());
                if (!string.IsNullOrEmpty(tbYSZEMax.Text.Trim()))
                    dc.Add("YSZEMax", tbYSZEMax.Text.Trim());
                using (CW_YSKZDBLL yskzdB = new CW_YSKZDBLL())
                {
                    int RecordCount = 0;
                    List<YSKZDModel> itemLists = yskzdB.GetYSKZD(dc, "BH desc", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                    this.AspNetPager1.RecordCount = RecordCount;
                    GridView3.DataSource = itemLists;
                    GridView3.DataBind();
                    this.AspNetPager1.Visible = RecordCount > 0;
                    panMsg.Visible = !this.AspNetPager1.Visible;
                }
            }
            else return;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
        }
        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        { }
        protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
        }
        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbIds = new StringBuilder();
            foreach (GridViewRow container in GridView1.Rows)
            {
                HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                if (cBox != null && cBox.Checked)
                    sbIds.Append(string.Format(",{0}", cBox.Value));
            }
            if (sbIds.Length > 0)
            {
                sbIds = sbIds.Remove(0, 1);
                using (XSHTBLL xBLL = new XSHTBLL())
                {
                    if (xBLL.Deletes("ID", sbIds.ToString()))
                        this.ShowMessage("删除成功");
                    else
                        this.ShowMessage("删除失败");
                }
                BindData();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}